﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MM.DBUtility;

namespace MobileManager
{
    public partial class CutIMSI : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            else
            {
                userModel = (MM.Model.T_Users)Session["CurUser"];
            }
            if (!IsPostBack)
            {
                inputDateFrom.Value = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                inputDateTo.Value = DateTime.Now.ToString("yyyy-MM-dd");

                GVDataBind();

                if (userModel != null && userModel.RoleID.Equals("1"))
                {
                    btnDelete.Visible = true;
                }
            }
        }
        #region GV Bind
        private void GVDataBind()
        {
            string strSQL = "SELECT * FROM (SELECT IMSI,CreateTime FROM T_CutSIM WITH(NOLOCK) WHERE 1=1 ";

            string strWhere = string.Empty;
            if (!string.IsNullOrEmpty(txtIMSI.Text.Trim()))
                strWhere += string.Format(" AND IMSI LIKE '%{0}%'", txtIMSI.Text.Trim());
            if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                strWhere += string.Format(" AND CreateTime BETWEEN '{0}' AND '{1}' ", inputDateFrom.Value, Convert.ToDateTime(inputDateTo.Value).AddDays(1).ToString("yyyy-MM-dd"));

            strSQL += strWhere + ") AS A";

            AspNetPager1.RecordCount = DbHelperSql.GetCount("SELECT COUNT(1) FROM (" + strSQL + ") AS B");

            string strOrder = " ORDER BY A.CreateTime DESC";

            if (AspNetPager1.CurrentPageIndex == 1)
                strSQL = string.Format(strSQL.Replace("SELECT *", "SELECT TOP {0} * "), AspNetPager1.PageSize) + strOrder;
            else
                strSQL = string.Format("SELECT TOP {0} * FROM ({1}) AS F WHERE rn BETWEEN {2} AND {3}", AspNetPager1.PageSize, strSQL.Replace("SELECT *", string.Format("SELECT *,row_number() over({0}) rn ", strOrder)), (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize - 1);

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            //ViewState["DataTemp"] = ds;
            lbCount.Text = "记录数：" + AspNetPager1.RecordCount;

        }
        private void GVDataBindAll()
        {
            string strSQL = "SELECT * FROM (SELECT IMSI,CreateTime FROM T_CutSIM WITH(NOLOCK) ) AS A";

            AspNetPager1.RecordCount = DbHelperSql.GetCount("SELECT COUNT(1) FROM (" + strSQL + ") AS B");

            string strOrder = " ORDER BY A.CreateTime DESC";

            if (AspNetPager1.CurrentPageIndex == 1)
                strSQL = string.Format(strSQL.Replace("SELECT *", "SELECT TOP {0} * "), AspNetPager1.PageSize) + strOrder;
            else
                strSQL = string.Format("SELECT TOP {0} * FROM ({1}) AS F WHERE rn BETWEEN {2} AND {3}", AspNetPager1.PageSize, strSQL.Replace("SELECT *", string.Format("SELECT *,row_number() over({0}) rn ", strOrder)), (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize - 1);

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            //ViewState["DataTemp"] = ds;
            lbCount.Text = "记录数：" + AspNetPager1.RecordCount;

        }
        protected void GVinfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //GVinfo.PageIndex = e.NewPageIndex;
            //GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
            //GVinfo.DataBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            GVDataBind();
        }
        #endregion


        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                {
                    if (Convert.ToDateTime(inputDateFrom.Value) > Convert.ToDateTime(inputDateTo.Value))
                    {
                        MM.Common.Common.PageStartAlert(this.Page, "请求时间From必须小于等于To！");
                        return;
                    }
                }
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnQueryAll_Click(object sender, EventArgs e)
        {
            try
            {
                inputDateFrom.Value = "";
                inputDateTo.Value = "";
                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string strSQL = "Delete FROM T_CutSIM Where 1=1";

                if (!string.IsNullOrEmpty(txtIMSI.Text.Trim()))
                    strSQL += string.Format(" AND IMSI LIKE '%{0}%'", txtIMSI.Text.Trim());
                if (!string.IsNullOrEmpty(inputDateFrom.Value) && !string.IsNullOrEmpty(inputDateTo.Value))
                    strSQL += string.Format(" AND CreateTime BETWEEN '{0}' AND '{1}' ", inputDateFrom.Value, Convert.ToDateTime(inputDateTo.Value).AddDays(1).ToString("yyyy-MM-dd"));

                DbHelperSql.ExecuteSql(strSQL);

                MM.Common.Common.PageStartAlert(this.Page, "删除成功！");

                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnADD_Click(object sender, EventArgs e)
        {
            try
            {
                string strIMSI = txtIMSI.Text.Trim();
                if (!string.IsNullOrEmpty(strIMSI))
                {
                    if (DbHelperSql.Exists(string.Format("SELECT 1 FROM T_CutSIM WITH(NOLOCK) WHERE IMSI='{0}'", strIMSI)))
                    {
                        MM.Common.Common.PageStartAlert(this.Page, "该IMSI已经存在，请重新添加！");
                    }
                    else
                    {
                        DbHelperSql.ExecuteSql(string.Format("INSERT INTO T_CutSIM(IMSI) VALUES('{0}')", strIMSI));
                        MM.Common.Common.PageStartAlert(this.Page, "添加成功！");
                        GVDataBindAll();
                    }
                }
                else MM.Common.Common.PageStartAlert(this.Page, "IMSI必须填写！");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
